package com.vint.mi;

import java.util.LinkedList;
import java.util.Queue;

/**
 * 1. done,easy
 * 2. 九宫格是否合法，不要求有解
 */
public class Mi54 {

    public static String solution(String line){
        String[] src = line.split(" ");
        char[][] me = new char[9][9];
        for(int i=0; i<9; i++){
            char[] ss = src[i].toCharArray();
            for(int j=0;j<18;j=j+2) {
                me[i][j/2] = ss[j];
            }
        }
        //横
        for(int i=0;i<9;i++){
            if(!isCorrect(me[i])){
               return "false";
            }
        }
        //竖
        for(int i=0;i<9;i++){
            boolean[] tag = new boolean[10];
            for(int j=0;j<10;j++)tag[j] = false;
            for(int j=0;j<9;j++){
                if(me[i][j]=='-')continue;
                if(tag[me[i][j]-'0']){
                    return "false";
                }else{
                    tag[me[i][j]-'0']=true;
                }
            }
        }
        //宫
        for(int i=0;i<9;i=i+3) {
            for (int j = 0; j < 9; j = j + 3) {
                if (!isPalaceCorrect(me, i, j)) {
                    return "false";
                }
            }
        }
        return "true";
    }
    public static boolean isPalaceCorrect(char[][] s,int ii, int jj){
        boolean[] tag = new boolean[10];
        for(int i=0;i<10;i++)tag[i] = false;
        for(int i=ii;i<ii+3;i++){
            for(int j=jj;j<jj+3;j++){
                if(s[i][j]=='-')continue;
                if(tag[s[i][j]-'0']){
                    return false;
                }else{
                    tag[s[i][j]-'0']=true;
                }
            }
        }
        return true;
    }

    public static boolean isCorrect(char[] s){
        if(s.length!=9){
            return false;
        }
        boolean[] tag = new boolean[10];
        for(int i=0;i<10;i++)tag[i] = false;
        for(int i=0;i<9;i++){
            if(s[i]=='-')continue;
            if(tag[s[i]-'0']){
                return false;
            }else{
                tag[s[i]-'0']=true;
            }
        }
        return true;
    }

    public static void main(String[] args) {
        String l1="5,3,-,6,-,-,-,9,8 -,7,-,1,9,5,-,-,- -,-,-,-,-,-,-,6,- 8,-,-,4,-,-,7,-,- -,6,-,8,-,3,-,2,- -,-,3,-,-,1,-,-,6 -,6,-,-,-,-,-,-,- -,-,-,4,1,9,-,8,- 2,8,-,-,-,5,-,7,9";
        String l2="5,3,-,6,-,-,-,9,8 -,7,-,1,9,5,-,-,- -,-,-,-,-,-,-,6,- 8,-,-,4,-,-,7,-,- -,6,-,8,-,3,-,2,- -,-,3,-,-,1,-,-,6 -,6,-,-,-,-,-,9,- -,-,-,4,1,9,-,8,- 2,8,-,-,-,5,-,7,9";
        System.out.println(solution(l1));
        System.out.println(solution(l2));
    }
}
